package ca.usherbrooke.domus.sipnmove;

import javax.swing.SwingUtilities;

import ca.usherbrooke.domus.sipnmove.call.impl.SipCallFactory;
import ca.usherbrooke.domus.sipnmove.call.view.impl.SipSocketCallViewFactory;
import ca.usherbrooke.domus.sipnmove.sip.SipFacade;
import ca.usherbrooke.domus.sipnmove.sip.impl.SipFacadeImpl;
import ca.usherbrooke.domus.sipnmove.useragent.controller.CallActionController;
import ca.usherbrooke.domus.sipnmove.useragent.controller.NewCallViewController;
import ca.usherbrooke.domus.sipnmove.useragent.controller.QuitActionController;
import ca.usherbrooke.domus.sipnmove.useragent.controller.SipUnknownCallController;
import ca.usherbrooke.domus.sipnmove.useragent.impl.UserAgentImpl;
import ca.usherbrooke.domus.sipnmove.useragent.view.UserAgentView;

/**
 * Entry point for a user agent
 */
public class UserAgentMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		/*********************************************
		 * Input parameters management
		 *********************************************/
		
		if (args.length < 3) {
			System.err.println("Not enough arguments :");
			System.err.println();
			System.err.println("[command] ip_address port_number user_name");
			System.err.println("	ip_address = Local IP address");
			System.err.println("	port_number = Local port number");
			System.err.println("	user_name = SIP user name");
			System.exit(-1);
		}

		String ipAddress = args[0];
		Integer port = new Integer(args[1]);
		String userName = args[2];
		
		/*********************************************
		 * Objects creation and dependencies injection
		 *********************************************/

		UserAgentImpl userAgent = new UserAgentImpl();
		
		SipSocketCallViewFactory sipSocketCallViewFactory = new SipSocketCallViewFactory();
		
			// Add the controller a new call events as a new call listener
			NewCallViewController newCallViewController = new NewCallViewController();
			newCallViewController.setCallViewFactory(sipSocketCallViewFactory);
			userAgent.addNewCallListener(newCallViewController);
	
		// Instantiation of the SipFacade
		SipFacade sipFacade = new SipFacadeImpl(ipAddress, port, userName);
	
			SipCallFactory sipCallFactory = new SipCallFactory();
			sipCallFactory.setSipFacade(sipFacade);
	
			SipUnknownCallController sipUnknownCallController = new SipUnknownCallController();
			sipUnknownCallController.setCallFactory(sipCallFactory);
			sipUnknownCallController.setUserAgent(userAgent);
			
		// Create view associated to the user agent
		final UserAgentView userAgentView = new UserAgentView(sipFacade);

			// Set call action controller
			CallActionController callActionController = new CallActionController();
			callActionController.setUserAgent(userAgent);
			callActionController.setUserAgentView(userAgentView);
			callActionController.setCallFactory(sipCallFactory);
			userAgentView.setCallActionController(callActionController);
	
			// Set quit controller
			QuitActionController quitController = new QuitActionController(userAgent);
			userAgentView.setQuitActionController(quitController);

		sipUnknownCallController.setUserAgentView(userAgentView);
		sipFacade.setUnknownCallSipFacadeListener(sipUnknownCallController);
			
		/*********************************************
		 * Display view
		 *********************************************/
		
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				userAgentView.display();
			}
		});

	}

}
